package edu.pitt.dbmi.odie.gapp.gwt.client.navigator;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.FormLayoutType;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.FormItem;
import com.smartgwt.client.widgets.form.fields.LinkItem;
import com.smartgwt.client.widgets.form.fields.SpacerItem;
import com.smartgwt.client.widgets.form.fields.StaticTextItem;
import com.smartgwt.client.widgets.form.fields.events.ClickHandler;
import com.smartgwt.client.widgets.layout.VLayout;

public class ODIE_MultiLineDocumentLinkManager {

	private static final int CONST_COLUMNS_PER_LINE = 80;

	private ArrayList<FormItem> formItems = new ArrayList<FormItem>();

	private boolean isFirstRowLink = true;

	private int currentColumnIndex = 0;

	public ODIE_MultiLineDocumentLinkManager() {
		;
	}

	public VLayout buildForm(VLayout form) {
		return buildFormWithFormItems(form, this.formItems);
	}

	protected VLayout buildFormWithFormItems(VLayout pageOfFormItems,
			ArrayList<FormItem> linkItems) {
		DynamicForm dynamicForm = buildDynamicForm();
		dynamicForm.setFields(convertToFormItemVector(linkItems));
		pageOfFormItems.addMember(dynamicForm);
		return pageOfFormItems;
	}

	private FormItem[] convertToFormItemVector(ArrayList<FormItem> linkItems) {
		final FormItem[] linkItemArray = new FormItem[linkItems.size()];
		int ldx = 0;
		for (FormItem linkItem : linkItems) {
			linkItemArray[ldx++] = linkItem;
		}
		return linkItemArray;
	}

	private DynamicForm buildDynamicForm() {
		DynamicForm form = new DynamicForm();
		form.setWidth(300) ;
		form.setHeight100() ;
		form.setCellBorder(0);
		form.setCellPadding(0);
		form.setCellSpacing(0);
		form.setFixedColWidths(true);
		form.setColWidths("10pt") ;
		form.setAlign(Alignment.LEFT) ;
		form.setMinColWidth(1);
		form.setTitleWidth(0);
		form.setItemLayout(FormLayoutType.TABLE);
		form.setNumCols(CONST_COLUMNS_PER_LINE);
		form.setPadding(0);

		// form.setItemLayout(FormLayoutType.ABSOLUTE);
		// form.setBorder("2px solid blue") ;
		// form.setTop(1) ;
		// form.setWidth("80%") ;
		return form;
	}

	// public static final String CONST_DOCUMENT_TEXT_STYLE =
	// "odie-DocumentText";
	// public static final String CONST_ANNOTATION_TEXT_STYLE =
	// "odie-Annotation";
	// public static final String CONST_ANNOTATION_SELECTED_STYLE =
	// "odie-Annotation-Selected" ;

	// SpacerItem
	// StaticTextItem
	// FormItem

	public List<FormItem> addFormItem(String htmlValue, String styleName) {
		List<FormItem> result = new ArrayList<FormItem>();
		if (htmlValue.length() == 0) {
			htmlValue = "&nbsp;";
		}
		String reducedValue = htmlValue.replaceAll("&nbsp;", " ");
		char[] reducedValueCharacters = reducedValue.toCharArray();
		for (char c : reducedValueCharacters) {
			FormItem formItem = null;
			if (styleName.equals(ODIE_DocumentViewer.CONST_DOCUMENT_TEXT_STYLE)) {
				formItem = new StaticTextItem();
				formItem
						.setTextBoxStyle(ODIE_DocumentViewer.CONST_DOCUMENT_TEXT_STYLE);
				formItem.setValue(c+"");
			} else if (styleName
					.equals(ODIE_DocumentViewer.CONST_ANNOTATION_TEXT_STYLE)) {
				formItem = new LinkItem();
				formItem
						.setTextBoxStyle(ODIE_DocumentViewer.CONST_ANNOTATION_TEXT_STYLE);
				formItem.setValue(c+"");
			} else if (styleName
					.equals(ODIE_DocumentViewer.CONST_ANNOTATION_SELECTED_STYLE)) {
				formItem = new LinkItem();
				formItem
						.setTextBoxStyle(ODIE_DocumentViewer.CONST_ANNOTATION_SELECTED_STYLE);
				formItem.setValue(c+"");
			}
			formItem.setWidth("10pt") ;
			formItem.setAlign(Alignment.LEFT) ;
			formItem.setTextBoxStyle(styleName);
			formItem.setColSpan(1);
			formItem.setShowTitle(false);
			if (isFirstRowLink) {
				formItem.setStartRow(true);
				isFirstRowLink = false;
			}
			result.add(formItem);
			formItems.add(formItem);
		}
		return result;
	}

	public void newRow() {
		while (this.currentColumnIndex < CONST_COLUMNS_PER_LINE) {
			SpacerItem spacerItem = new SpacerItem();
			this.formItems.add(spacerItem);
			this.currentColumnIndex++;
		}
		isFirstRowLink = true;
		this.currentColumnIndex = 0;
	}

	public List<FormItem> addFormItem(ArrayList<Character> htmlBuffer,
			String styleName) {
		String result = "";
		for (Iterator<Character> resultIterator = htmlBuffer.iterator(); resultIterator
				.hasNext();) {
			result += resultIterator.next();
		}
		return addFormItem(result, styleName);
	}

	public List<FormItem> addClickableFormItem(ArrayList<Character> htmlBuffer,
			String constAnnotationTextStyle, ClickHandler clickHandler) {
		List<FormItem> linkItems = addFormItem(htmlBuffer,
				constAnnotationTextStyle);
		for (FormItem formItem : linkItems) {
			formItem.addClickHandler(clickHandler);
		}
		return linkItems;
	}

	public ArrayList<FormItem> getFormItemsByStyle(String style) {
		ArrayList<FormItem> result = new ArrayList<FormItem>();
		for (FormItem linkItem : this.formItems) {
			String candidateStyle = linkItem.getTextBoxStyle();
			if (candidateStyle != null && style != null
					&& candidateStyle.equals(style)) {
				result.add(linkItem);
			}
		}
		return result;
	}

	protected VLayout buildFormWithFormItemsOld(VLayout pageOfLineForms,
			ArrayList<FormItem> linkItems) {
		ArrayList<FormItem> linksForLine = null;
		DynamicForm formForLine = null;
		if (linkItems.size() > 0) {
			for (FormItem linkItem : linkItems) {
				if (linkItem.getStartRow()) {
					if (formForLine != null) {
						formForLine
								.setFields(convertToFormItemVector(linksForLine));
						pageOfLineForms.addMember(formForLine);
					}
					linksForLine = new ArrayList<FormItem>();
					formForLine = buildDynamicForm();
				}
				linksForLine.add(linkItem);
			}
			if (linksForLine != null && !linksForLine.isEmpty()) {
				if (formForLine == null) {
					formForLine = buildDynamicForm();
				}
				formForLine.setFields(convertToFormItemVector(linksForLine));
				pageOfLineForms.addMember(formForLine);
			}
		}
		return pageOfLineForms;
	}

	protected int calculateAbsoluteCharacters(String htmlValue) {
		String reducedValue = htmlValue.replaceAll("&nbsp;", " ");
		double len1 = reducedValue.length();
		len1 = (3.0d / 4.0d) * len1;
		int result = (int) Math.ceil(len1);
		result = Math.max(1, result);
		return result;
	}

}
